/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.DisciplineBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.PlanBO;
import org.ipti.br.tag.bo.SeasonBO;
import org.ipti.br.tag.entity.Discipline;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.Plan;
import org.ipti.br.tag.entity.Season;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author israel
 */
@ManagedBean(name = "createOrganizationPlan")
@ViewScoped
public class CreateOrganizationPlan extends AppBaseBean{

    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    private Plan plan;
    private List<SelectItem> disciplineList;
    private List<SelectItem> organizationList;
    private List<SelectItem> seasonList;
    private List<SelectItem> yearList;
    private Long disciplineID;
    private Long organizationID;
    private Long seasonID;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private DisciplineBO disciplineBO;
    @Inject
    private SeasonBO seasonBO;
    @Inject
    private PlanBO planBO;
    private boolean success;
    
    private final int INITIAL_YEAR = 1986;

    public CreateOrganizationPlan() {
    }

    public void initPlan() {
        plan = new Plan();
        success = false;
    }
    
    public void redirectNewRegister() {
        redirect("/educational/plans/createOrganizationPlans.xhtml");
    }
    
    public void create() {
        Discipline discipline = new Discipline();
        discipline.setId(disciplineID);
        Organization organization = new Organization();
        organization.setId(organizationID);
        Season season = new Season();
        season.setId(seasonID);
        
        plan.setDiscipline(discipline);
        plan.setOrganization(organization);
        plan.setSeason(season);
        planBO.create(plan);
        success = true;
    }
    
    public void loadDisciplineList() {
        disciplineList = new ArrayList<SelectItem>();
        disciplineList.add(new SelectItem(null, "Selecione uma Disciplina"));
        for(Discipline discipline : disciplineBO.findAll()) {
            disciplineList.add(new SelectItem(discipline.getId(), discipline.getName()));
        }
    }

    public void loadOrganizationList() {
        organizationList = new ArrayList<SelectItem>();
        organizationList.add(new SelectItem(null, "Selecione uma Organização"));
        for (Organization org : organizationBO.getOrganizationDescedents(sessionBean.getLoggedActor().getUnity().getOrganization().getId())) {
            organizationList.add(new SelectItem(org.getId(), org.getName()));
        }
    }
    
    public void loadSeasonList() {
        seasonList = new ArrayList<SelectItem>();
        seasonList.add(new SelectItem(null, "Selecione um Período"));
        for(Season season : seasonBO.findAll()) {
            seasonList.add(new SelectItem(season.getId(), season.getDescription()));
        }
    }
    
    public void loadYearList() {
        yearList = new ArrayList<SelectItem>();
        yearList.add(new SelectItem(null, "Selecione um Ano"));
        for(int i = INITIAL_YEAR; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
            yearList.add(new SelectItem(i, String.valueOf(i)));
        }
    }

    public Long getDisciplineID() {
        return disciplineID;
    }

    public void setDisciplineID(Long disciplineID) {
        this.disciplineID = disciplineID;
    }

    public List<SelectItem> getDisciplineList() {
        if(disciplineList == null) {
            loadDisciplineList();
        }
        return disciplineList;
    }

    public void setDisciplineList(List<SelectItem> disciplineList) {
        this.disciplineList = disciplineList;
    }

    public Long getOrganizationID() {
        return organizationID;
    }

    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }

    public List<SelectItem> getOrganizationList() {
        if (organizationList == null) {
            loadOrganizationList();
        }
        return organizationList;
    }

    public void setOrganizationList(List<SelectItem> organizationList) {
        this.organizationList = organizationList;
    }

    public Plan getPlan() {
        return plan;
    }

    public void setPlan(Plan plan) {
        this.plan = plan;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Long getSeasonID() {
        return seasonID;
    }

    public void setSeasonID(Long seasonID) {
        this.seasonID = seasonID;
    }

    public List<SelectItem> getSeasonList() {
        if(seasonList == null) {
            loadSeasonList();
        }
        return seasonList;
    }

    public void setSeasonList(List<SelectItem> seasonList) {
        this.seasonList = seasonList;
    }

    public DisciplineBO getDisciplineBO() {
        return disciplineBO;
    }

    public void setDisciplineBO(DisciplineBO disciplineBO) {
        this.disciplineBO = disciplineBO;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public SeasonBO getSeasonBO() {
        return seasonBO;
    }

    public void setSeasonBO(SeasonBO seasonBO) {
        this.seasonBO = seasonBO;
    }

    public List<SelectItem> getYearList() {
        if(yearList == null) {
            loadYearList();
        }
        return yearList;
    }

    public void setYearList(List<SelectItem> yearList) {
        this.yearList = yearList;
    }

    public PlanBO getPlanBO() {
        return planBO;
    }

    public void setPlanBO(PlanBO planBO) {
        this.planBO = planBO;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
    
}
